﻿Imports System.Data
Imports System.IO
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Edit
	Inherits System.Web.UI.UserControl
	Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region " PRIVARTE SUB AND FUNCTION"
	Sub ShowData()
		Using data As New VSEntities
			Dim p As MSDSOriginal = (From q In data.MSDSOriginals Where q.Id = hidID.Value Select q).SingleOrDefault
			If Not p Is Nothing Then
				txtItemcode.Text = IIf(IsNothing(p.ItemCode) = True, "", p.ItemCode)
				hidItemCode.Value = txtItemcode.Text
				txtItemname.Text = IIf(IsNothing(p.ItemName) = True, "", p.ItemName)
                hidVendorCode.Text = IIf(IsNothing(p.VendorCode) = True, "", p.VendorCode)
				txtVendorname.Text = IIf(IsNothing(p.Vendorname) = True, "", p.Vendorname)
                txtOriginal.Text = IIf(IsNothing(p.Original) = True, "", p.Original)
                txtURL.Text = IIf(IsNothing(p.URL) = True, "", p.URL)
                txtPPE.Text = IIf(IsNothing(p.PPE) = True, "", p.PPE)

				Dim _originalFile As String = IIf(IsNothing(p.OriginalFile) = True, "", p.OriginalFile)
                hidImagePath.Text = _originalFile
                hplFile.NavigateUrl = _originalFile
                hplFile.Text = _originalFile

                Dim _TranslateFile As String = IIf(IsNothing(p.TraslateFile) = True, "", p.TraslateFile)
                hidImagePath2.Text = _TranslateFile
                hplFile2.NavigateUrl = _TranslateFile
                hplFile2.Text = _TranslateFile

			End If
        End Using
	End Sub
#End Region
#Region "EVENT CONTROL-PAGE"
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not IsPostBack Then
			Me.EnableViewState = False
			Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
			If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
				ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
			Else
				Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
			End If
			If Not Request.QueryString("Id").ToString.Equals("0") Then
				hidID.Value = Request.QueryString("Id")
				ShowData()
			End If
		End If
	End Sub

    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click



        Using data As New VSEntities
            Try
                Dim p As MSDSOriginal
                p = (From q In data.MSDSOriginals Where q.Id = hidID.Value).FirstOrDefault
                'check exist item and vendor

                Dim exist = (From q In data.MSDSOriginals Where q.ItemCode = txtItemcode.Text And q.VendorCode = hidVendorCode.Text And q.Id <> hidID.Value Select q).FirstOrDefault
                If Not exist Is Nothing Then
  
                    Excute_Javascript("AlertboxRedirect('Item và vendor đã tồn tại trong hệ thống.','../../Page/MSDS/Edit.aspx?Id=" + hidID.Value + "');", Me.Page, True)
                    Exit Sub
                End If



                If Not p Is Nothing Then
                    Dim _error As String = ""
                    With p
                        .ItemCode = txtItemcode.Text.Trim
                        .ItemName = txtItemname.Text.Trim
                        .VendorCode = hidVendorCode.Text
                        .Vendorname = txtVendorname.Text.Trim
                        .Original = txtOriginal.Text.Trim
                        .OriginalFile = hidImagePath.Text
                        .TraslateFile = hidImagePath2.Text
                        .URL = txtURL.Text
                        .PPE = txtPPE.Text
                        .Modifier = Session("UserName")
                        .Modified = Now.Date
                    End With
                    data.SaveChanges()

                    Insert_App_Log("Update  MSDSOriginal:" & txtItemname.Text.Trim & "", Function_Name.MSDSOriginal, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                    Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công." & _error & "','../../Page/MSDS/List.aspx');", Me.Page, True)
                End If
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub

	Protected Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
		Response.Redirect("List.aspx")
	End Sub
#End Region
End Class
